package com.metacube.questionbank.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.model.User;
import com.metacube.questionbank.service.impl.AnswerServiceImpl;
import com.metacube.questionbank.service.impl.QuestionServiceImpl;
import com.metacube.questionbank.service.impl.TagServiceImpl;
import com.metacube.questionbank.service.impl.UserServiceImpl;

/*
 * @author Team Neeraj Jain
 * */

/*
 * This controller handles request that comes for posting a question.
 * */
@Controller
public class AskQuestionController {
	@Autowired
	QuestionServiceImpl questionService;
	@Autowired
	TagServiceImpl tagService;
	@Autowired
	AnswerServiceImpl answerService;
	@Autowired
	UserServiceImpl userService;

	/*
	 * @param req This method handles the request that comes for the ask
	 * question page.It returns the view of ask question page.
	 * 
	 * @return view of ask question page.
	 */
	@RequestMapping(value = "askquestion.htm")
	public String displayAskQuestion(HttpServletRequest req) {
		List<User> users = userService.getUsers();
		req.setAttribute("users", users);
		return "askquestion";
	}

	/*
	 * @param req This method handles the request that comes for the posting the
	 * question.It returns the view of questions page with that question added
	 * to the questions page.
	 * 
	 * @return view of question page.
	 */
	@RequestMapping(value = "questions.htm", method = RequestMethod.POST)
	public String postQuestion(HttpServletRequest req) {
		Question questionObj = new Question();
		String questionTitle = req.getParameter("title");
		questionObj.setQuestionTitle(questionTitle);
		questionObj.setQuestionDesc(req.getParameter("description"));
		int userId = Integer.parseInt(req.getParameter("id"));
		questionObj.setUserId(userId);
		questionObj.setClosingStatus(true);
		questionService.insertQuestion(questionObj);
		int maxQId = questionService.getMaxQId();
		tagService.insertQuestionTags(req.getParameter("tags"), maxQId);
		List<Question> questions = questionService.getQuestions();
		List<Integer> answers = answerService.getAnswerCount(questions);
		List<List<Tag>> tags = tagService.getQuestionTags(questions);
		List<User> userList = userService.getUserQId(questions);
		req.setAttribute("questions", questions);
		req.setAttribute("ansCount", answers);
		req.setAttribute("tags", tags);
		req.setAttribute("userList", userList);
		return "questions";

	}

	/*
	 * @param question title of question This method handles ajax request.When
	 * the user types the question title,it returns the related questions.
	 * 
	 * @return questions related questions.
	 */
	@RequestMapping(value = "pi.json", method = RequestMethod.GET)
	public @ResponseBody List<Question> showRelatedQuestions(
			@RequestParam(value = "question") String question,
			HttpServletRequest req) {
		List<Question> questions = questionService
				.getRelatedQuestions(question);
		return questions;

	}

	/*
	 * @param req This method shows the related questions when the user searches
	 * something in the search bar.
	 * 
	 * @return view of questions page that contains related questions
	 */
	@RequestMapping(value = "searchResult.htm", method = RequestMethod.POST)
	public String displaySearchResult(HttpServletRequest req) {
		String search = req.getParameter("search");
		List<Question> questions = questionService.getRelatedQuestions(search);
		List<Integer> answerCount = answerService.getAnswerCount(questions);
		List<List<Tag>> tags = tagService.getQuestionTags(questions);
		List<User> userList = userService.getUserQId(questions);
		req.setAttribute("questions", questions);
		req.setAttribute("ansCount", answerCount);
		req.setAttribute("tags", tags);
		req.setAttribute("userList", userList);
		return "questions";
	}
}